﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;
using Galaktika.BI.Extensibility.Dom.Dac;
using Galaktika.BI.Silverlight.Controls.DateChoice.ClientServer;
using Galaktika.BI.Silverlight.ClientApp.Web.Commands.MemberChoice;
using Ranet.Olap.Core.Data;
using Ranet.Web.Olap;
using Ranet.Olap.Core;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands.DateChoice
{
    public class DateChoiceCommand : ServerCommand
    {
        public DateChoiceCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.DateChoiceCommandId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.ConnectionId = invoker[KnownInvokeArgs.ConnectionId];
            this.Args = invoker[KnownInvokeArgs.DateChoiceQueryInfo];
        }

        public string ContextId { get; private set; }
        public string ConnectionId { get; private set; }
        public string Args { get; private set; }

        private IBindingContext Context
        {
            get
            {
                return DataBinder.Current.GetContext(this.ContextId);
            }
        }

        OlapDataManager m_Manager = null;
        private OlapDataManager DataManager
        {
            get
            {
                if (m_Manager == null)
                {
                    BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
                    if (conn != null)
                    {
                        m_Manager = new OlapDataManager(new ConnectionInfo(conn.Name, conn.ConnectionString), String.Empty, String.Empty);
                    }
                }
                return m_Manager;
            }
        }

        public override Galaktika.BI.Runtime.Services.InvokeResultSchema Invoke()
        {
            try
            {
                InvokeResultSchema res = new InvokeResultSchema(base.Id);
                DateChoiceQueryArgs args = XmlUtility.XmlStr2Obj<DateChoiceQueryArgs>(this.Args);

                if (DataManager != null)
                {
                    return GetDays(args);
                }

                res.ContentType = KnownContentTypes.MultidimData;
                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        InvokeResultSchema GetDays(DateChoiceQueryArgs args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            OlapProvider provider = new OlapProvider(new MDXQueryExecuter(DataManager.Connection, Context));

            List<MemberDataWrapper> members = new List<MemberDataWrapper>();

            foreach (String monthUniqueName in args.MonthUniqueNames)
            {
                members.AddRange(provider.GetChildrenMembers(args.CubeName, String.Empty, monthUniqueName));
            }

            res.ContentType = KnownContentTypes.MultidimData;
            res.Content = XmlUtility.Obj2XmlStr(members, Common.Namespace);

            return res;
        }
    }
}
